package com.shijie.sjoj.service.core;

import com.shijie.sjoj.model.api.quiz.QuizService;
import com.shijie.sjoj.model.component.IdGeneratorRedis;
import com.shijie.sjoj.model.component.validate.NumberValidate;
import com.shijie.sjoj.model.exception.QuizException;
import com.shijie.sjoj.model.pojo.entity.Judge;
import com.shijie.sjoj.model.pojo.entity.Quiz;
import com.shijie.sjoj.model.utils.StringUtils;
import com.shijie.sjoj.service.mapper.QuizMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

/**
 * 题目服务接口实现类
 *
 * @author ZSJ
 */
@Slf4j
@DubboService
@RequiredArgsConstructor
public class QuizServiceImpl implements QuizService {

    /**
     * 题目DAO接口
     */
    private final QuizMapper quizMapper;

    /**
     * 全局id生成器
     */
    private final IdGeneratorRedis idGenerator;

    @Override
    public List<Quiz> list() {
        // 查询题目
        return quizMapper.list();
    }

    @Override
    public Quiz getByQuizId(Long quizId) {
        // 无效值校验
        if (NumberValidate.invalidSys(quizId)) {
            throw QuizException.throwError("无效的题目id");
        }
        // 查询题目
        return quizMapper.getByQuizId(quizId);
    }

    @Override
    public Judge obtainJudge(Long quizId) {
        // 无效值校验
        if (NumberValidate.invalidSys(quizId)) {
            throw QuizException.throwError("无效的题目id");
        }
        // 查询题目
        return quizMapper.getJudgeRequireInfo(quizId);
    }

    @Override
    public boolean saves(List<Quiz> quizzes) {
        // 无效值校验
        if (null == quizzes || quizzes.isEmpty()) {
            throw QuizException.throwError("无效的题目列表");
        }
        // 过滤无效题目
        List<Quiz> validQuizzes = quizzes.stream()
                .filter(q -> (NumberValidate.validZero(q.getQuizNum())
                        && StringUtils.notBlank(q.getTitle())
                        && null != q.getDifficulty()
                        && StringUtils.notBlank(q.getParamsType())
                        && StringUtils.notBlank(q.getIncomePath())
                        && StringUtils.notBlank(q.getOutcomePath())
                        // TODO 分类验证方式待定
                        && StringUtils.notBlank(q.getCategory())))
                .toList();
        // 题目格式化
        validQuizzes.forEach(q -> q.setQuizId(idGenerator.nextQuizId()));
        // 新增题目
        return quizMapper.saves(validQuizzes);
    }

    @Override
    public boolean modify(Quiz quiz) {
        // 无效值校验
        if (NumberValidate.invalidSys(quiz.getQuizId())) {
            throw QuizException.throwError("无效的题目id");
        }
        // 题目格式化
        quiz.setTitle(StringUtils.isBlank(quiz.getTitle()) ? null : quiz.getTitle());
//        quiz.setDifficulty(null == quiz.getDifficulty() ? null : quiz.getDifficulty());
        quiz.setDescription(StringUtils.isBlank(quiz.getDescription()) ? null : quiz.getDescription());
        quiz.setCategory(StringUtils.isBlank(quiz.getCategory()) ? null : quiz.getCategory());
        // 更新题目
        return quizMapper.modify(quiz);
    }

    @Override
    public boolean destroys(List<Long> quizIds) {
        // 移除题目
        return quizMapper.destroys(quizIds);
    }


}
